package com.huike.utils;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

public class CreateExcelUtils {
    private final static String url="D:/template.xls";
    public static void createExcel(Integer createRow,Integer createColumn){
        // 创建工作簿对象
        Workbook workbook = new XSSFWorkbook();

        // 创建工作表对象
        Sheet sheet = workbook.createSheet("Template");

        // 创建行和列
        int rowCount = createRow; // 行数
        int columnCount = createColumn; // 列数

        for (int rowIdx = 0; rowIdx < rowCount; rowIdx++) {
            Row row = sheet.createRow(rowIdx);
            for (int colIdx = 0; colIdx < columnCount; colIdx++) {
                Cell cell = row.createCell(colIdx);
//                cell.setCellValue(1);
            }
        }

        // 将工作簿保存为文件
        String filePath = url; // 文件路径
        try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
            try {
                workbook.write(outputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("Excel 模板下载成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 关闭工作簿
        try {
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
